#import "CTInputView.h"
@interface CTInputView ()
@property (nonatomic ,assign) BOOL showLeftView;
@property (nonatomic ,strong) UIView *whiteLine;
@property (nonatomic ,strong) NSDate *lastDate;
@end
@implementation CTInputView
- (instancetype)init {
    return [self initWithShowLeftView:NO];
}
- (instancetype)initWithShowLeftView:(BOOL)showLeftView
{
    if (self = [super init]) {
        self.showLeftView = showLeftView;
        [self setUI];
    }
    return self;
}
#pragma mark - private
- (void)setUI
{
    self.tfModel = [[CTTFModel alloc] init];
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.backView];
    if (_showLeftView) {
        [_backView addSubview:self.leftLabel];
        [_backView addSubview:self.contentTF];
    } else {
        [_backView addSubview:self.contentTF];
    }
    [self.backView addSubview:self.clearBtn];
    [self.backView addSubview:self.whiteLine];
    if (self.contentTF.text.length == 0) {
        self.clearBtn.hidden = YES;
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(self);
    }];
    [self.clearBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.backView).offset(Ratio(-18));
        make.centerY.equalTo(self.backView);
        make.size.mas_equalTo(CGSizeMake(Ratio(24), Ratio(24)));
    }];
    if (_showLeftView) {
        [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Ratio(20));
            make.centerY.equalTo(self->_backView);
            make.width.mas_equalTo(Ratio(56));
        }];
        [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.leftLabel.mas_right).offset(Ratio(10));
            make.right.mas_equalTo(Ratio(-10));
            make.centerY.equalTo(self->_backView);
        }];
    } else {
        [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Ratio(10));
            make.right.mas_equalTo(Ratio(-10));
            make.centerY.equalTo(self->_backView);
        }];
    }
    [self.whiteLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.leftLabel.mas_right).offset(Ratio(-8));
        make.top.bottom.equalTo(self.backView);
        make.width.mas_equalTo(Ratio(3));
    }];
}
- (void)callKeyBoard
{
    [self.contentTF becomeFirstResponder];
}
- (UILabel *)leftLabel {
    if (!_leftLabel) {
        _leftLabel = [[UILabel alloc] init];
        _leftLabel.text = @"+234";
        _leftLabel.textColor = [UIColor color4E5460];
        _leftLabel.font = [UIFont systemFontOfSize:Ratio(14)];
    }
    return _leftLabel;
}
- (UITextField *)contentTF
{
    if (_contentTF == nil) {
        _contentTF = [[UITextField alloc] init];
        _contentTF.keyboardType = UIKeyboardTypeNumberPad;
        _contentTF.font = [UIFont boldSystemFontOfSize:Ratio(12)];
        _contentTF.textColor = [UIColor color77777D];
        NSString *holderText = @"Please enter your phone number";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttributes:@{NSFontAttributeName:
                                     [UIFont boldSystemFontOfSize:Ratio(12)],
                                     NSForegroundColorAttributeName:[UIColor color77777D]
        }
                            range:NSMakeRange(0, holderText.length)];
        _contentTF.attributedPlaceholder = placeholder;
        @weakify(self);
        [[_contentTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
            @strongify(self);
            self.clearBtn.hidden = (self.contentTF.text.length > 0) ? NO : YES;
            if (self.editingBlock) {
                self.editingBlock(self, self.contentTF.text);
            }
            NSDate *currentDate = [NSDate date];
            NSTimeInterval interval = [NSDate timeIntervalWithStart:self.lastDate end:currentDate];
            NSString *newLog = [NSString stringWithFormat:@"%.2f:%@:0,",interval, self.contentTF.text];
            [self.tfModel.valueParameter appendString:newLog];
            self.lastDate = currentDate;
            NSLog(@"val:-----%@", self.tfModel.valueParameter);
        }];
        [[_contentTF rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
            @strongify(self);
            self.lastDate = [NSDate date];
            if (self.tfModel.dwellDuration == 0) {
                self.tfModel.initiationTimestamp = [self.lastDate timeIntervalSince1970];
            }
            NSString *newLog = [NSString stringWithFormat:@"%d:%@:0,",0,  self.contentTF.text].mutableCopy;
            [self.tfModel.valueParameter appendString:newLog];
        }];
        [[_contentTF rac_signalForControlEvents:UIControlEventEditingDidEnd] subscribeNext:^(id x) {
            @strongify(self);
            NSDate *currentDate = [NSDate date];
            NSTimeInterval interval = [NSDate timeIntervalWithStart:self.lastDate end:currentDate];
            NSString *newLog = [NSString stringWithFormat:@"%f:%@:2,",interval,  self.contentTF.text];
            [self.tfModel.valueParameter appendString:newLog];
            self.lastDate = currentDate;
            self.tfModel.conclusionTimestamp = [currentDate timeIntervalSince1970];
            self.tfModel.dwellDuration = [NSDate timeIntervalWithStartInterval:self.tfModel.initiationTimestamp endInterval:self.tfModel.conclusionTimestamp];
            self.lastDate = currentDate;
            NSLog(@"val:-----%@", self.tfModel.valueParameter);
            if (self.editDidEndBlock) {
                self.editDidEndBlock(self, self.tfModel);
            }
        }];
    }
    return _contentTF;
}
- (UIView *)backView
{
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor colorF0F2F5];
        _backView.layer.masksToBounds = YES;
        _backView.layer.cornerRadius = Ratio(23);
    }
    return _backView;
}
- (UIButton *)clearBtn
{
    if (_clearBtn == nil) {
        _clearBtn = [[UIButton alloc] init];
        [_clearBtn setImage:[UIImage imageNamed:@"input_clear"] forState:UIControlStateNormal];
        [_clearBtn setImage:[UIImage imageNamed:@"input_clear"] forState:UIControlStateSelected];
        @weakify(self);
        [[_clearBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            self.contentTF.text = @"";
            self.clearBtn.hidden = YES;
        }];
    }
    return _clearBtn;
}
- (UIView *)whiteLine
{
    if (!_whiteLine) {
        _whiteLine = [[UIView alloc] init];
        _whiteLine.backgroundColor = [UIColor whiteColor];
    }
    return _whiteLine;
}
@end
